package tn.orange.canreminder.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import tn.orange.canreminder.conf.Config;
import tn.orange.canreminder.model.Gps;
import tn.orange.canreminder.model.JSONPlaces;
import tn.orange.canreminder.model.Place;
import tn.orange.canreminder.parse.GenericJsonParser;

public class PlaceServiceImp implements PlaceService {

	GenericJsonParser parser = new GenericJsonParser();

	@Override
	public List<Place> sendGPS(Gps gps) throws Exception {
		// TODO Auto-generated method stub

		parser.setOutput(JSONPlaces.class);
		parser.setUrl(Config.places);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("longitude", ""
				+ gps.getLongitude()));
		nameValuePairs.add(new BasicNameValuePair("latitude", ""
				+ gps.getLatitude()));
		nameValuePairs.add(new BasicNameValuePair("category_id", ""
				+ gps.getCategory_id()));

		nameValuePairs
				.add(new BasicNameValuePair("rayon", "" + gps.getRayon()));

		parser.setNameValuePairs(nameValuePairs);

		JSONPlaces jsonplaces = (JSONPlaces) parser.parse();

		List<Place> places = jsonplaces.getPlaces();

		RatingService ratingService = new RatingServiceImp();
		for (Place place : places) {
            place.setRating(ratingService.getRatingbyId(place.getId()));
		}

		return places;

	}

}
